<template>
  <div class="search">
    <div class="search__input-container">
      <van-search v-model="value" placeholder="请输入站点名称" @input="handleSearch" shape="round" @clear="clearSearch" />
      <van-list :finished="finished" finished-text="" class="search__result">
        <van-cell v-for="station in stations" :key="station.id" :title="station.fullPath" @click="() => handleStation(station)" />
      </van-list>
    </div>

    <!-- 最近搜索记录 -->
    <div class="search__recent">
      <div class="search__recent-title">
        <div class="search__recent-title-text">最近搜索</div>
        <div class="search__recent-title-clear" @click="handleClear">
          <van-icon name="delete-o" size="18" />
        </div>
      </div>
      <div class="search__recent-divider"></div>
      <div class="search__recent-list">
        <div class="search__recent-item" v-for="item in recentSearch" :key="item" @click="() => handleRecent(item)">
          <van-icon name="clock-o" size="14" />
          {{ item }}
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import Input from '@/components/input';
import { storage } from '@/modules/storage';

@Component({
  name: 'SearchStation',
  components: {
    Input
  }
})
export default class SearchStation extends Vue {
  private value = '';
  private recentSearch: string[] = [];
  private stations: Record<string, any> = [];
  // private stations: Record<string, any> = [];
  private finished = false;

  mounted() {
    this.getLocalRecentSearch();
  }

  handleClear() {
    this.recentSearch = [];
    storage.remove('recentSearch');
  }

  getLocalRecentSearch() {
    const recentSearch = storage.get('recentSearch') || '[]';
    this.recentSearch = JSON.parse(recentSearch);
  }

  setLocalRecentSearch() {
    // 去重以及最多6个
    if (this.value === '' || this.recentSearch.includes(this.value)) return;
    this.recentSearch = [this.value].concat(this.recentSearch).slice(0, 6);
    storage.set('recentSearch', JSON.stringify(this.recentSearch));
  }

  handleRecent(item: string) {
    this.value = item;
    this.handleSearch();
  }

  clearSearch() {
    this.finished = false;
    this.value = '';
    this.stations = [];
  }

  handleSearch() {
    if (this.value === '') {
      this.finished = false;
      this.stations = [];
      return;
    };
    const bureauCode = window.map.Config?.User?.bureauCode ?? '0301';
    const queryCondition = {
      powerGridFlag: 1,
      flName: this.value
    }
    this.$toast.loading({ message: '正在搜索变电站信息', duration: 0 });
    this.finished = false;
  }

  handleStation(station: Record<string, any>) {
    this.$store.dispatch('setStation', {
      flName: station.flName,
      id: station.id,
    })
    this.setLocalRecentSearch();
    this.$router.push({ name: 'SearchDevice' });
  }
}
</script>

<style lang="scss" scoped>
.search {
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;

  &__input-container {
    width: 100%;
    position: relative;
    background-color: #fff;
  }

  &__recent {
    padding: 12px;

    &-title {
      display: flex;
      justify-content: space-between;
      font-size: 14px;
      color: #8c8c8c;

      &-text {
        padding: 8px 0;
      }

      &-clear {
        padding: 8px 0 8px 12px;
      }
    }

    &-divider {
      height: 1px;
      background-color: #ccc;
    }

    &-list {
      margin: 8px 0;
      display: flex;
      color: #8c8c8c;
      flex-wrap: wrap;
    }

    &-item {
      width: 50%;
      font-size: 14px;
      line-height: 24px;
    }
  }

  &__result {
    position: absolute;
    width: 100%;
    background-color: #fff;
    z-index: 10;
  }

  &__station {
    background-color: #fff;
    font-size: 14px;
    color: #333;
    line-height: 24px;
    padding: 4px 12px 6px;
  }
}
</style>
